//
//  FeeCalculatorViewController.m
//  homeland
//
//  Created by caesar on 14-8-5.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "FeeCalculatorViewController.h"

@interface FeeCalculatorViewController ()
//变量区
//=========================
//定义一个变量来记录下一个将要添加的控件的位置
@property (assign, nonatomic) int nextY;
//党员类别
@property (strong, nonatomic) NSString *partyTypeStr;
//收入类别
@property (strong, nonatomic) NSString *incomeTypeStr;
@end

@implementation FeeCalculatorViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"党费计算";
    [self configView];
}

/**数据渲染展示*/
-(void)configView{
    //元素添加边框
    //--------------------------
    self.mainPanel.layer.borderWidth = 1;
    self.mainPanel.layer.borderColor = [[UIColor blackColor] CGColor];
    self.mainPanel.layer.cornerRadius = 0;
    /**
    self.partyTypePanel.layer.borderWidth = 1;
    self.partyTypePanel.layer.borderColor = [[UIColor blackColor] CGColor];
    self.partyTypePanel.layer.cornerRadius = 0;
    
    self.incomeTypePanel.layer.borderWidth = 1;
    self.incomeTypePanel.layer.borderColor = [[UIColor blackColor] CGColor];
    self.incomeTypePanel.layer.cornerRadius = 0;
    
    self.feePanel.layer.borderWidth = 1;
    self.feePanel.layer.borderColor = [[UIColor blackColor] CGColor];
    self.feePanel.layer.cornerRadius = 0;
    
    self.incomePanel.layer.borderWidth = 1;
    self.incomePanel.layer.borderColor = [[UIColor blackColor] CGColor];
    self.incomePanel.layer.cornerRadius = 0;
    */
    self.incomeText.layer.borderWidth = 1;
    self.incomeText.layer.borderColor = [[UIColor blackColor] CGColor];
    self.incomeText.layer.cornerRadius = 5;
    
    self.partyTypeBtn.layer.borderWidth = 1;
    self.partyTypeBtn.layer.borderColor = [[UIColor blackColor] CGColor];
    self.partyTypeBtn.layer.cornerRadius = 5;
    
    self.incomeTypeBtn.layer.borderWidth = 1;
    self.incomeTypeBtn.layer.borderColor = [[UIColor blackColor] CGColor];
    self.incomeTypeBtn.layer.cornerRadius = 5;
    
    //加载下拉列表数据
    //--------------------------
    [self buildPartyTypeData];
    [self buildIncomeTypeData];
    
    _nextY+= (CGRectGetMaxY(self.feePanel.frame) + 10.0);//下一元素显示的位置
    
    //创建按钮
    [self createBtn];
    
    CGFloat totalHeight = MAX(CGRectGetMaxY(self.feePanel.frame), CGRectGetHeight(self.scrollView.frame));
    self.mainPanel.frame = CGRectMake(0.0, 0.0, 320.0, totalHeight);
    [self.scrollView addSubview:self.mainPanel];
    self.scrollView.contentSize = self.mainPanel.frame.size;
}

/**
 *创建按钮
 */
- (void)createBtn{
    int height = iPhone5 ? 0 : 88 ;
    //生成底部按扭-
    UIButton *postBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 460 - height, 320, 44)];
    [postBtn setBackgroundImage:[UIImage imageNamed:@"btnImage.png"] forState:0];
    [postBtn setTitle:@"计算" forState:0];
    [postBtn addTarget:self action:@selector(postBtnClicked) forControlEvents:64];
    [self.view addSubview:postBtn];
}

//提交事件
- (void)postBtnClicked{
    if ( self.partyTypeStr.length == 0) {
        [self.view makeToast:@"请选择党员类别" duration:2.0 position:@"center"];
        return;
    }
    if(self.incomeTypeStr.length == 0){
        [self.view makeToast:@"请选择收入类别" duration:2.0 position:@"center"];
        return;
    }
    
    NSString *feeStr = [CommonUtil dealNullObject:self.incomeText.text];
     if([@"在岗职工" equals:self.partyTypeStr]||[@"离退休干部、职工" equals:self.partyTypeStr]){
         if ([feeStr equals:@""]) {
             [self.view makeToast:@"请输入收入" duration:2.0 position:@"center"];
             return;
         }
     }
    [self caculatePartyFee:feeStr.floatValue];
    
}

/**计算党费*/
- (void)caculatePartyFee:(float)income{
    [self feeResult:income];
}
//党员类别选择框事件
- (IBAction)partyTypeSelectClicked:(id)sender {
    
    if(partyTypeDropDown == nil) {//展开
        CGFloat f = 280;
        partyTypeDropDown = [[NIDropDown alloc]init];
        partyTypeDropDown = [[NIDropDown alloc]showDropDown:sender cHeight:&f nArr:self.partyTypeList bIsSub:YES];
        partyTypeDropDown.delegate = self;
        partyTypeDropDown.tag = 0;
    }else {//收缩
        [partyTypeDropDown hideDropDown:sender];
        [self rel];
    }
}
//收入类别选择框事件
- (IBAction)incomeTypeSelectClicked:(id)sender {
        if(incomeTypeDropDown == nil) {//展开
            CGFloat f = 120;
            incomeTypeDropDown = [[NIDropDown alloc]init];
            incomeTypeDropDown = [[NIDropDown alloc]showDropDown:sender cHeight:&f nArr:self.incomeTypeList bIsSub:YES];
            incomeTypeDropDown.delegate = self;
            incomeTypeDropDown.tag = 1;
        }else {//收缩
            [incomeTypeDropDown hideDropDown:sender];
            [self rel];
        }
}
/**下拉选择方法*/
- (void) niDropDownDelegateMethod: (NIDropDown *) sender indexPath:(NSInteger *)index{
    if (sender.tag == 0) {
        //选择党员类别
        DropDownEntity *dde = [self.partyTypeList objectAtIndex:(int)index];
        self.partyTypeStr = @"";
        self.partyTypeStr = [NSString stringWithFormat:@"%@",dde.name];
    }else{
        //选择收入类别
        DropDownEntity *dde = [self.incomeTypeList objectAtIndex:(int)index];
        self.incomeTypeStr = @"";
        self.incomeTypeStr = [NSString stringWithFormat:@"%@",dde.name];
    }
    
    if([@"在岗职工" equals:self.partyTypeStr]||[@"离退休干部、职工" equals:self.partyTypeStr]){
        self.incomePanel.hidden = NO;
        self.feePanel.frame = CGRectMake(self.feePanel.frame.origin.x,CGRectGetMaxY(self.incomePanel.frame), self.feePanel.frame.size.width, self.feePanel.frame.size.height);
    }else{
        self.incomePanel.hidden = YES;
        self.feePanel.frame = CGRectMake(self.feePanel.frame.origin.x,CGRectGetMaxY(self.incomeTypePanel.frame), self.feePanel.frame.size.width, self.feePanel.frame.size.height);
    }
    [self rel];
}

//释放下拉框内存
-(void)rel{
    partyTypeDropDown = nil;
    incomeTypeDropDown = nil;
}

//构建党员类别下拉列表数据
- (void)buildPartyTypeData{
    NSMutableArray *tempArray = [NSMutableArray array];
    DropDownEntity *dde;

    dde = [DropDownEntity alloc];
    dde.Id = @"1";
    dde.name = @"在岗职工";
    [tempArray addObject:dde];

    dde = [DropDownEntity alloc];
    dde.Id = @"2";
    dde.name = @"离退休干部、职工";
    [tempArray addObject:dde];
    
    dde = [DropDownEntity alloc];
    dde.Id = @"3";
    dde.name = @"农民";
    [tempArray addObject:dde];
    
    dde = [DropDownEntity alloc];
    dde.Id = @"4";
    dde.name = @"学生";
    [tempArray addObject:dde];
    
    dde = [DropDownEntity alloc];
    dde.Id = @"5";
    dde.name = @"下岗失业";
    [tempArray addObject:dde];
    
    dde = [DropDownEntity alloc];
    dde.Id = @"6";
    dde.name = @"依靠抚恤或救济生活";
    [tempArray addObject:dde];
    
    dde = [DropDownEntity alloc];
    dde.Id = @"7";
    dde.name = @"领取当地最低生活保障金";
    [tempArray addObject:dde];
    
    self.partyTypeList = tempArray;
}

//构建收入类别下拉列表数据
- (void)buildIncomeTypeData{
    NSMutableArray *tempArray = [NSMutableArray array];
    DropDownEntity *dde;
    
    dde = [DropDownEntity alloc];
    dde.Id = @"1";
    dde.name = @"月";
    [tempArray addObject:dde];
    
    dde = [DropDownEntity alloc];
    dde.Id = @"2";
    dde.name = @"季度";
    [tempArray addObject:dde];
    
    dde = [DropDownEntity alloc];
    dde.Id = @"3";
    dde.name = @"年";
    [tempArray addObject:dde];
    self.incomeTypeList = tempArray;
}

 /**
 * 计算应缴党费
 * @param income 收入（税后）
 */
-(void) feeResult:(float) income{
    NSString *feeResult=@"";
    float fee =0;
    if([@"在岗职工" equals:self.partyTypeStr]){
        if([@"月" equals:self.incomeTypeStr]){
            if(income<=3000){
                fee = income*0.005;
            }else if(income>3000 && income<=5000){
                fee = income*0.01;
            }else if(income>5000&& income<=10000){
                fee = income*0.015;
            }
            else{
                fee = income*0.02;
            }
        }else if([@"季度" equals:self.incomeTypeStr]){
            if(income<=3000){
                fee = income*0.005*3;
            }else if(income>3000 && income<=5000){
                fee = income*0.01*3;
            }else if(income>5000&& income<=10000){
                fee = income*0.015*3;
            }
            else{
                fee = income*0.02*3;
            }
        }else{
            if(income<=3000){
                fee = income*0.005*12;
            }else if(income>3000 && income<=5000){
                fee = income*0.01*12;
            }else if(income>5000&& income<=10000){
                fee = income*0.015*12;
            }
            else{
                fee = income*0.02*12;
            }
        }
    }else if([@"离退休干部、职工" equals:self.partyTypeStr]){
        if([@"月" equals:self.incomeTypeStr]){
            if(income<=5000){
                fee = income*0.005;
            }
            else{
                fee = income*0.01;
            }
        }else if([@"季度" equals:self.incomeTypeStr]){
            if(income<=5000){
                fee = income*0.005*3;
            }
            else{
                fee = income*0.01*3;
            }
        }else{
            if(income<=5000){
                fee = income*0.005*12;
            }else{
                fee = income*0.01*12;
            }
        }
        
    }else if([@"农民" equals:self.partyTypeStr]){
        if([@"月" equals:self.incomeTypeStr]){
            feeResult=@"0.2－1";
        }else if([@"季度" equals:self.incomeTypeStr]){
            feeResult=@"0.6－3";
        }else{
            feeResult=@"2.4－12";
        }
    }else if([@"学生" equals:self.partyTypeStr]||[@"下岗失业" equals:self.partyTypeStr]||[@"依靠抚恤或救济生活" equals:self.partyTypeStr]||[@"领取当地最低生活保障金" equals:self.partyTypeStr]){
        if([@"月" equals:self.incomeTypeStr]){
            feeResult=@"0.2";
        }else if([@"季度" equals:self.incomeTypeStr]){
            feeResult=@"0.6";
        }else{
            feeResult=@"2.4";
        }
    }
    if (fee > 0) {
        feeResult= [CommonUtil roundUp:fee afterScale:2];
    }
    self.feeText.text = feeResult;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
